package com.king.wms.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.king.wms.pojo.Loc;
import com.king.wms.pojo.Sku;
import com.king.wms.pojo.Storer;
import com.king.wms.service.LocService;
import com.king.wms.service.SkuService;
import com.king.wms.service.StorerService;

@Controller
@RequestMapping(value = "/sku")
public class SkuController {

	@Autowired
	private SkuService skuService;

	@Autowired
	private StorerService storerService;
	
	@Autowired
	private LocService locService;
	

	@RequestMapping("/toInsertSkuPage")
	public void toInsertSkuPage(HttpServletResponse response,
			HttpServletRequest request) throws ServletException, IOException {
		
		request.setAttribute("list", storerService.findAll());
		request.getRequestDispatcher("/jsp/sku/insertSku.jsp").forward(request,
				response);

	}

	@RequestMapping(value = "/insert")
	public String insert(Sku sku,
			HttpServletRequest request) throws ServletException, IOException {
		String message = skuService.insert(sku);
		
		if("success".equals(message)){
			return "redirect:/sku/findAll";
		}else
		{
			request.setAttribute("message", message);
			
			return "error";
		}
		
		
	}

	@RequestMapping("/toUpdateSkuPage/{sku_id}")
	public void toUpdateStorerPage(@PathVariable("sku_id") Integer sku_id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		Sku sku = skuService.findSkuBySku_id(sku_id);
		request.setAttribute("sku", sku);
		
		List<Loc> list_loc = locService.findAll();
		List<Storer> list_storer = storerService.findAll();
		
		//request.setAttribute("list", storerService.findAll());
		System.out.println(list_loc);
		System.out.println(list_storer);
		
		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_storer", list_storer);

		request.getRequestDispatcher("/jsp/sku/updateSku.jsp").forward(request,
				response);

	}

	@RequestMapping(value = "/update")
	public String update(Sku sku) {
		skuService.update(sku);
		return "redirect:/sku/findAll";
	}

	@RequestMapping(value = "/delete/{sku}")
	public String delete(@PathVariable Integer sku) {
		skuService.delete(sku);
		
		return "redirect:/sku/findAll";
	}

	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<Sku> list_sku = skuService.findAll();
		List<Loc> list_loc = locService.findAll();
		List<Storer> list_storer = storerService.findAll();
		
		//request.setAttribute("list", storerService.findAll());
		System.out.println(list_sku);
		System.out.println(list_loc);
		System.out.println(list_storer);
		
		request.setAttribute("list_sku", list_sku);
		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_storer", list_storer);
		request.getRequestDispatcher("/jsp/sku/sku.jsp").forward(request,
				response);

	}
	@RequestMapping(value = "/findAll1")
	public void findAll1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		List<Sku> list_sku = skuService.findAll();
		for(Sku sku:list_sku){
			String[] s1 = sku.getTime().split("-");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String[] s2 = sdf.format(new Date()).split("-");

			double s11 = Integer.parseInt(s1[0])*12+Integer.parseInt(s1[1])+Integer.parseInt(s1[2])/30.0;
			double s12 = Integer.parseInt(s2[0])*12+Integer.parseInt(s2[1])+Integer.parseInt(s2[2])/30.0;
			if(sku.getBaozhiqi()-(s12-s11)<sku.getBaozhiqi()/2){
				sku.setFlag(true);
			}else{
				sku.setFlag(false);
			}
		}
		List<Loc> list_loc = locService.findAll();
		List<Storer> list_storer = storerService.findAll();

		//request.setAttribute("list", storerService.findAll());
		System.out.println(list_sku);
		System.out.println(list_loc);
		System.out.println(list_storer);

		request.setAttribute("list_sku", list_sku);
		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_storer", list_storer);
		request.getRequestDispatcher("/jsp/sku/sku1.jsp").forward(request,
				response);

	}

	@RequestMapping(value = "/findAll2")
	public void findAll2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		List<Sku> list_sku = skuService.findAll();
		Set<String> set1 = new HashSet<>();
		for(Sku sku:list_sku) {
			set1.add(sku.getCategory());
		}
       String s1 = "['product',";
		for(String s:set1){
			s1+="'"+s+"',";
		}
        s1 = s1.substring(0,s1.length())+"]";

		Map<String,Integer> map = new HashMap<>();
		for(Sku sku:list_sku) {
			if(map.containsKey(sku.getCategory())){
				map.replace(sku.getCategory(),map.get(sku.getCategory())+sku.getQuantity());
			}else{
				map.put(sku.getCategory(),sku.getQuantity());
			}
		}

		String s2 = "['商品类型',";
        for(String key:map.keySet()){
        	s2 += map.get(key)+",";
		}
		s2 = s2.substring(0,s2.length())+"]";

		request.setAttribute("s1", s1);
		request.setAttribute("s2", s2);
		request.getRequestDispatcher("/jsp/sku/sku2.jsp").forward(request,
				response);

	}

}
